Ext.ns('ru.proburo.oms.components.form');

/**
 * @class ru.proburo.oms.components.form.SearchField
 * @extends Ext.form.TwinTriggerField
 */
ru.proburo.oms.components.form.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent : function(){
        ru.proburo.oms.components.form.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                this.onTrigger2Click();
            }
        }, this);
    },

    validationEvent:false,
    validateOnBlur:false,
    trigger1Class:'x-form-clear-trigger',
    trigger2Class:'x-form-search-trigger',
    hideTrigger1:true,
    width:180,
    hasSearch : false,
    paramName : 'query',

    onTrigger1Click : function()
    {
        this.reset();
        this._query();
    },

    onTrigger2Click : function()
    {
        this.store.baseParam = this.store.baseParam || {};
        
        var p = this.store.baseParam[this.paramName];
        var v = this.getRawValue();
        
        if(v && v != p)
        {
            this._query();
        }
        else if(p)
        {
            this.onTrigger1Click();
        }
    },
    
    // private
    _query: function()
    {
        var q = this.getRawValue();
        this.store.setBaseParam(this.paramName, q);
        this.store.reload({params: {start: 0}});
        
        var m = (!q) ? 'hide' : 'show';
        this.triggers[0][m]();
    }
});